<template>
  <div class="home" :style="{ background: 'url(' + personImg + ') no-repeat' }">
    <div class="back" @click="goPage('home')">返回</div>
    <div class="ranking" :style="{ background: 'url(' + r3 + ') no-repeat' }">
      <div class="rankingBox" v-if="isTime">
        <div class="progressBox">
          <md-progress
            class="progress"
            :value="currentTimer / countDown"
            :width="20"
            :size="80"
            color="url(#linear)"
            border-color="#FFF"
            linecap="butt"
          >
            <span class="progress-value">{{ currentTimer }}s</span>
            <defs slot="defs">
              <linearGradient id="linear" x1="0%" y1="0%" x2="100%" y2="0%">
                <stop offset="0%" stop-color="#FFF098" />
                <stop offset="100%" stop-color="#EF9889" />
              </linearGradient>
            </defs>
          </md-progress>
        </div>
        <div>
          <p>{{ currentIndex + 1 }}、{{ currentList[currentIndex].text }}</p>
          <ol>
            <li
              @click="question(index)"
              v-for="(item, index) in currentList[currentIndex].info"
              :key="index"
              :class="[
                rightIndex === index ? 'success' : '',
                errorIndex === index ? 'error' : ''
              ]"
            >
              {{ item }}
            </li>
          </ol>
        </div>
      </div>
      <div class="messageBox" v-else>
        <h1>您来的太早啦！</h1>
        <p>
          活动时间: 大年初一至大年初七（1月25日-1月31日）,
          每天早上10点到晚上24点
        </p>
      </div>
    </div>
    <md-landscape v-model="showPic" @hide="goPage('home')">
      <img v-if="rightNum === 0" src="@/assets/img/question0.png" />
      <img v-else-if="rightNum === 1" src="@/assets/img/question1.png" />
      <img v-else-if="rightNum === 2" src="@/assets/img/question2.png" />
      <img v-else-if="rightNum === 3" src="@/assets/img/question3.png" />
      <img v-else-if="rightNum === 4" src="@/assets/img/question4.png" />
      <img v-else-if="rightNum === 5" src="@/assets/img/question5.png" />
    </md-landscape>
  </div>
</template>

<script>
import wx from "weixin-jsapi";
import personImg from "@/assets/img/questionBg1.jpg";
import questionList from "@/tool/question";
import r3 from "@/assets/img/questionBg2.png";
import {
  Popup,
  Landscape,
  Button,
  Toast,
  PopupTitleBar,
  Amount,
  Progress
} from "mand-mobile";
export default {
  name: "index",
  components: {
    [Amount.name]: Amount,
    [Progress.name]: Progress,
    [Popup.name]: Popup,
    [Landscape.name]: Landscape,
    [Button.name]: Button,
    [PopupTitleBar.name]: PopupTitleBar
  },
  data() {
    return {
      personImg: personImg,
      questionList: questionList,
      r3: r3,
      timer: null,
      countDown: 15,
      currentTimer: 15,
      currentList: [],
      currentIndex: 0,
      rightNum: 0,
      rightIndex: -1,
      errorIndex: -1,
      disabled: false,
      showPic: false,
      isTime: true
    };
  },
  methods: {
    question(index) {
      if (this.disabled) {
        return false;
      }
      if (this.info.answer) {
        return false;
      }
      this.disabled = true;
      if (this.currentIndex < 5) {
        this.rightIndex = this.currentList[this.currentIndex].value - 1;
        if (this.currentList[this.currentIndex].value === index + 1) {
          this.rightNum++;
        } else {
          this.errorIndex = index;
        }
      }
      this.restart();
    },
    goPage(routeName) {
      this.$router.push({ name: routeName });
    },
    logout() {
      localStorage.removeItem("mtoken");
      sessionStorage.clear();
      this.$router.push({ name: "login" });
    },
    getConfig() {
      wx.ready(() => {
        this.wxShare();
      });
    },
    start() {
      this.timer = setInterval(() => {
        if (this.currentTimer > 0) {
          this.currentTimer--;
          let info = {
            id: this.info.id,
            currentTimer: this.currentTimer,
            currentDate: this.getNowFormatDate(),
            disabled: this.disabled,
            errorIndex: this.errorIndex,
            rightIndex: this.rightIndex,
            currentIndex: this.currentIndex,
            showPic: this.showPic,
            rightNum: this.rightNum
          };
          localStorage.setItem("question", JSON.stringify(info));
        } else {
          this.disabled = true;
          this.restart();
        }
      }, 1000);
    },
    restart() {
      clearInterval(this.timer);
      if (this.currentIndex < 4) {
        setTimeout(() => {
          this.currentTimer = 15;
          this.errorIndex = -1;
          this.rightIndex = -1;
          this.currentIndex++;
          this.start();
          this.disabled = false;
        }, 800);
      } else {
        setTimeout(() => {
          this.currentTimer = 15;
          this.submitQuestion();
        }, 800);
      }
      let info = {
        id: this.info.id,
        currentDate: this.getNowFormatDate(),
        disabled: this.disabled,
        errorIndex: this.errorIndex,
        rightIndex: this.rightIndex,
        currentIndex: this.currentIndex,
        showPic: this.showPic,
        rightNum: this.rightNum
      };
      localStorage.setItem("question", JSON.stringify(info));
    },
    submitQuestion() {
      this.$ajax
        .post(this.$api.answer, { num: this.rightNum }, true)
        .then(() => {
          this.showPic = true;
          let info = {
            id: this.info.id,
            currentDate: this.getNowFormatDate(),
            disabled: this.disabled,
            errorIndex: this.errorIndex,
            rightIndex: this.rightIndex,
            currentIndex: this.currentIndex,
            showPic: this.showPic,
            rightNum: this.rightNum
          };
          localStorage.setItem("question", JSON.stringify(info));
        })
        .catch(err => {
          Toast.failed(err, 3000, true);
        });
    },
    wxShare() {
      let self = this;
      const share_title = "答题押电影，豪礼领不停！";
      const share_desc =
        "是时候展现你的票房预测能力了，签到再答题，福鼠天天送，瓜分新年豪礼！";
      const share_link = "http://gift.hrycf.com";
      const share_img = "https://test.hrycf.com/hry/gift/cj.png";
      wx.showOptionMenu();
      wx.onMenuShareTimeline({
        title: share_title, // 分享标题
        link: share_link, // 分享链接
        imgUrl: share_img
      });
      wx.onMenuShareAppMessage({
        title: share_title, // 分享标题
        desc: share_desc, // 分享描述
        link: share_link, // 分享链接
        imgUrl: share_img,
        success: function() {
          self.getOneOpportunity();
        }
      });
    },
    getNowFormatDate() {
      var date = new Date();
      var seperator1 = "-";
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = year + seperator1 + month + seperator1 + strDate;
      return currentdate;
    }
  },
  created() {
    let currentDate = this.getNowFormatDate();
    var curDate = new Date();
    let currentHour = curDate.getHours();
    let timestamp = new Date().getTime();
    if (
      currentHour > 9 &&
      (timestamp > 1579917600000 && timestamp < 1580486399000)
    ) {
      if (this.info.answer) {
        this.rightNum = this.info.answer_num;
        this.showPic = true;
      } else {
        let info = localStorage.getItem("question");
        if (info) {
          info = JSON.parse(info);
          if (this.info.id === info.id) {
            if (currentDate === info.currentDate) {
              this.disabled = info.disabled;
              this.errorIndex = info.errorIndex;
              this.rightIndex = info.rightIndex;
              this.currentIndex = info.currentIndex;
              this.showPic = info.showPic;
              this.rightNum = info.rightNum;
              this.currentTimer = info.currentTimer;
            }
          }
        }
        if (this.showPic) {
          this.currentTimer = 0;
        } else {
          this.start();
        }
      }
      if (currentDate === "2020-01-25") {
        this.currentList = this.questionList[0];
      } else if (currentDate === "2020-01-26") {
        this.currentList = this.questionList[1];
      } else if (currentDate === "2020-01-27") {
        this.currentList = this.questionList[2];
      } else if (currentDate === "2020-01-28") {
        this.currentList = this.questionList[3];
      } else if (currentDate === "2020-01-29") {
        this.currentList = this.questionList[4];
      } else if (currentDate === "2020-01-30") {
        this.currentList = this.questionList[5];
      } else if (currentDate === "2020-01-31") {
        this.currentList = this.questionList[6];
      }
    } else {
      this.isTime = false;
    }
  },
  mounted() {
    this.getConfig();
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  computed: {
    isValidate() {
      return this.$store.state.isValidate;
    },
    isLogin() {
      return this.$store.state.isLogin;
    },
    info() {
      return this.$store.state.info;
    }
  }
};
</script>

<style scoped lang="less">
.home {
  height: 100%;
  width: 100%;
  position: relative;
  padding: 280px 0 0 0;
  background-size: 100%, 100% !important;
  @media (max-width: 320px) {
    padding: 30px 0 0 0;
  }
  .back {
    position: fixed;
    padding: 20px 50px;
    top: 20px;
    left: 30px;
    font-weight: bold;
    color: white;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 40px;
  }
  .ranking {
    padding: 22px 52px;
    width: 704px;
    height: 956px;
    margin: 0 auto;
    background-size: 100%, 100% !important;
    .rankingBox {
      width: 100%;
      height: 100%;
      overflow-y: auto;
      font-size: 26px;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(102, 102, 102, 1);
      .progress-value {
        font-size: 28px;
        font-family: PingFang SC;
        font-weight: bold;
        color: rgba(196, 7, 5, 1);
        line-height: 36px;
      }
      .progressBox {
        text-align: center;
      }
      .progress {
        display: inline-block;
      }
      ol {
        padding: 0;
        margin: 0;
      }
      p {
        font-size: 30px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(45, 0, 0, 1);
        line-height: 38px;
        margin-bottom: 60px;
      }
      li {
        list-style-type: upper-alpha;
        list-style-position: inside;
        padding: 24px;
        border: 2px solid rgba(255, 255, 255, 1);
        border-radius: 38px;
        margin-bottom: 30px;
        font-size: 26px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(45, 0, 0, 1);
        line-height: 36px;
      }
      .success {
        background: linear-gradient(
          -18deg,
          rgba(242, 73, 21, 1) 0%,
          rgba(250, 129, 19, 1) 100%
        );
      }
      .error {
        background: linear-gradient(
          -12deg,
          rgba(169, 169, 169, 1) 0%,
          rgba(95, 95, 95, 1) 100%
        );
      }
    }
    .messageBox {
      h1 {
        line-height: 120px;
        text-align: center;
        font-size: 40px;
        font-weight: bold;
      }
      p {
        line-height: 60px;
        font-size: 30px;
      }
    }
  }
}
</style>
